package com.wjt.homestayms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjt.homestayms.common.Result;
import com.wjt.homestayms.mapper.TenantMapper;
import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.pojo.Tenant;
import com.wjt.homestayms.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class TenantServiceImpl implements ITenantService {

    @Autowired
    TenantMapper tenantMapper;

    @Override
    public boolean addTenant(Tenant tenant) {
        return tenantMapper.addTenant(tenant);
    }

    @Override
    public boolean updateTenant(Tenant tenant) {
        List<Tenant> tenantList = tenantMapper.getTenantByTid(tenant.gettId());
        if (tenantList.size() > 0) {
            return tenantMapper.updateTenant(tenant);
        }
        return false;
    }

    @Override
    public boolean deleteTenant(Integer tId) {
        List<Tenant> tenantList = tenantMapper.getTenantByTid(tId);
        if (tenantList.size() > 0) {
            return tenantMapper.deleteTenant(tId);
        }
        return false;
    }

    @Override
    public Result<Tenant> getById(Integer tId) {
        LambdaQueryWrapper<Tenant> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Tenant::gettId,tId);

        return Result.ok(tenantMapper.selectOne(queryWrapper));
    }

    @Override
    public List<Tenant> getTenantById(Integer tId) {
        return tenantMapper.getTenantByTid(tId);
    }

    @Override
    public PageResult getTenantList(PageRequest pageRequest, Integer id) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Tenant> tenantList = tenantMapper.getTenantList(id);
        return getPageResult(tenantList);
    }

    @Override
    public PageResult getTenantByName(PageRequest pageRequest, String tName) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Tenant> tenantList = tenantMapper.getTenantByName(tName);
        return getPageResult(tenantList);
    }

    private PageResult getPageResult(List<Tenant> list) {
        //传入的list长度大于0，表示list中有数据存在。
        if (list.size() > 0) {
            PageInfo<Tenant> pageInfo = PageInfo.of(list);
            PageResult page = new PageResult();
            //获取数据记录总数
            page.setTotal(pageInfo.getTotal());
            //获取当前页面页码
            page.setPageNum(pageInfo.getPageNum());
            //获取当前页面数据的个数
            page.setPageSize(pageInfo.getPageSize());
            //获取页面总数
            page.setTotalPage(pageInfo.getPages());
            //封装页面内容
            Iterator<Tenant> iterator = pageInfo.getList().iterator();
            List<Object> content = new ArrayList<>();
            while (iterator.hasNext()) {
                Tenant tenant = iterator.next();
                content.add(tenant);
            }
            page.setContent(content);
            //封装完成返回PageResult对象
            return page;
        } else {
            return null;
        }
    }

    /**
     * 根据id获取用户信息。
     * @param tId
     * @return
     */
    public Tenant getTenatByid(Integer tId){
        List<Tenant> tenants = tenantMapper.getTenantByTid(tId);
        Tenant tenant = tenants.get(0);
        return tenant;
    }
}
